﻿var cookieName = "offerId";
var domainName = ".alibaba.com";
var prefix = "";
var defaultClassName = "offer";
var checkedClassName = "offer_select";
var categoryId = 0;
var inside_cooperate_id = false;
var tempCount = 0;
var url;

document.cookie  = cookieName + "="+"";
//判断是否超出比较的条数以及
function clickcompareBox(id,box) {
    if(box.checked){
        if(getOfferIdCount()>=10 && !inside_cooperate_id){
            alert('您已经选择了10条供应信息，候选供应信息不能超过10个');
            box.checked=false;
        }else{
            addOfferId(id, box);
        }
    } else {
        delOfferId(id);
   }
}

//有几条要比较的商机
function getOfferIdCount() {
    value = getCookie(cookieName);
    if(value == null || value == 'null') {
        return 0;
    }
    var count = 1;
    while(value.indexOf(",") != -1) {
        var position = value.indexOf(",");
        var len = value.length;
        value =  value.substring(position+1,len);
        count++;
    }
    return count;
}

function getCookie(name) {
    var arg = name + "=";
    var alen = arg.length;
    var clen = document.cookie.length;
    var i=0;
    while(i<clen) {
        var j=i+alen;
        if (document.cookie.substring(i, j) == arg) {
            return www_helpor_net(j);
        }
        i=document.cookie.indexOf(" ",i)+1;if(i==0)
        break;
    }
    return null;
}

function www_helpor_net(offset) {
    var endstr=document.cookie.indexOf(";",offset);
    if (endstr == -1) {
        endstr = document.cookie.length;
    }
    return unescape(document.cookie.substring(offset,endstr));
}

//添加到比较信息里
function addOfferId(arg, box) {
    arg  = arg + prefix;
    if(hasOfferId(arg)) {
        alert(getCookie(cookieName));
        //box.checked=false;
        return;
    }
    if(arg == "") {
        return;
    }
    value = getCookie(cookieName);
    if(value == null || value == "null" ) {
        value = arg ;
    } else {
        value=value+","+ arg;
    }
    cookieValue = cookieName + "=" + value+";hehe="+value; 
    document.cookie  = cookieValue;
    var countId = box.id.substring("compareBox".length,box.id.length);
}

//判断是否存在该信息
function hasOfferId(arg) {
    var value = getCookie(cookieName);
    if(value == null || value == "null" || value.indexOf(arg) == -1 ) {
        return false;
    } else {
        return true;
    }
}

function delOfferId(arg) {
    arg = arg + prefix;
    if(!hasOfferId(arg)) {
        alert("要删除的商机不存在");
        return;
    }
    value = getCookie(cookieName);
    var offerIds = stringToArray(value);
    var len = offerIds.length;
    var result = "";
    var counter = 0;
    for(var i=0;i<len;i++) {
        if(offerIds[i] != arg) {
            if(counter == 0) {
                result = offerIds[i];
            } else {
                result = result + "," + offerIds[i];
            }
            counter++;
        }
    }
    cookieValue = cookieName + "=" + result;
    document.cookie = cookieValue;
}

function stringToArray(str) {
    var temp = str;
    var strArray = new Array();
    var count = 0;
    while(temp.indexOf(",") != -1) {
        var position = temp.indexOf(",");
        strArray[count] = temp.substring(0,position);
        count++;
        var len = temp.length;
        temp =  temp.substring(position+1,len);
    }
    strArray[count] = temp;
    return strArray;
}


//开始比较
function doCompare(type){
    if(getOfferIdCount()<=1){
        alert('请至少选择2条信息进行对比!');
    }else if(getOfferIdCount()>10){
        alert('您已经选择了10条供应信息，候选供应信息不能超过10个');
    }else{
        window.open("../search/compare.aspx?type="+ type+"&cpid="+getCookie(cookieName));
    }
}

//开始询价
function doInquiry(type){
    if(getOfferIdCount()<1){
        alert('请至少选择1条信息!');
        return false;
    }
    window.open("../search/Inquiry.aspx?type="+ type+"&cpid="+getCookie(cookieName));
}

//开始报价
function doQuotation(type){
    if(getOfferIdCount()<1){
        alert('请至少选择1条信息!');
        return false;
    }
    window.open("../search/Quotation.aspx?type="+ type+"&cpid="+getCookie(cookieName));
}